//
//  ForgetPasswordViewController.m
//  UserUse
//
//  Created by angra on 16/10/13.
//  Copyright © 2016年 CJPC. All rights reserved.
//

#import "ForgetPasswordViewController.h"

@interface ForgetPasswordViewController ()
@property (nonatomic,retain)dispatch_source_t timer;
@property (nonatomic,weak)UITextField *phoneTF;
@property (nonatomic,weak)UITextField *codeTF;
@property (nonatomic,weak)UITextField *passwordTF;
@property (nonatomic,weak)UIButton *submitBtn;
@end

@implementation ForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"忘记密码";
    [self setInterface];
}
-(void)setInterface
{
    UIImageView *phoneImage = [[UIImageView alloc]init];
    phoneImage.image = [UIImage imageNamed:@"cellphonenumber"];
    [self.view addSubview:phoneImage];
    [phoneImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(kScaleNum(33));
        make.size.mas_equalTo(CGSizeMake(kScaleNum(10), kScaleNum(16)));
        make.left.equalTo(self.view).with.offset(kScaleNum(35));
    }];
    
    UITextField *phoneTF = [[UITextField alloc]init];
    [phoneTF addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
    phoneTF.placeholder = @"请输入手机号";
    phoneTF.tag = 400;
    phoneTF.keyboardType = UIKeyboardTypePhonePad;
    phoneTF.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:phoneTF];
    [phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneImage.mas_right).with.offset(kScaleNum(18));
        make.height.equalTo(phoneImage);
        make.right.equalTo(self.view).with.offset(-kScaleNum(35));
        make.bottom.equalTo(phoneImage.mas_bottom).with.offset(1);
    }];
    _phoneTF = phoneTF;
    
    UIView *lineView1 = [[UIView alloc]init];
    lineView1.backgroundColor = [UIColor colorWithHexString:@"e5e5e5"];
    [self.view addSubview:lineView1];
    [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(phoneImage.mas_left);
        make.right.equalTo(phoneTF.mas_right);
        make.height.equalTo(@1);
        make.top.equalTo(phoneImage.mas_bottom).with.offset(kScaleNum(5));
    }];
    
    UIImageView *codeImage = [[UIImageView alloc]init];
    codeImage.image = [UIImage imageNamed:@"password"];
    [self.view addSubview:codeImage];
    [codeImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView1.mas_bottom).with.offset(kScaleNum(20));
        make.size.mas_equalTo(CGSizeMake(kScaleNum(12), kScaleNum(16)));
        make.left.equalTo(self.view).with.offset(kScaleNum(35));
    }];
    
    UITextField *codeTF = [[UITextField alloc]init];
    [codeTF addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
    codeTF.placeholder = @"请输入验证码";
    codeTF.tag = 401;
    codeTF.secureTextEntry = YES;
    codeTF.font = [UIFont systemFontOfSize:14];
    codeTF.keyboardType = UIKeyboardTypePhonePad;
    [self.view addSubview:codeTF];
    [codeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(codeImage.mas_right).with.offset(kScaleNum(15));
        make.height.equalTo(codeImage);
        make.right.equalTo(self.view).with.offset(-kScaleNum(35)-kScaleNum(125/2));
        make.bottom.equalTo(codeImage.mas_bottom).with.offset(1);
    }];
    _codeTF = codeTF;
    
    UIView *lineView2 = [[UIView alloc]init];
    lineView2.backgroundColor = [UIColor colorWithHexString:@"e5e5e5"];
    [self.view addSubview:lineView2];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(codeImage.mas_left);
        make.right.equalTo(lineView1.mas_right);
        make.height.equalTo(@1);
        make.top.equalTo(codeImage.mas_bottom).with.offset(kScaleNum(5));
    }];
    
    UIButton *codeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    codeBtn.backgroundColor = [UIColor colorWithHexString:@"cccccc"];
    [codeBtn addTarget:self action:@selector(getCode:) forControlEvents:UIControlEventTouchUpInside];
    [codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [codeBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
    codeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:codeBtn];
    [codeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lineView1);
        make.width.equalTo(@(kScaleNum(125/2)));
        make.height.equalTo(@(kScaleNum(24)));
        make.bottom.equalTo(lineView2.mas_top).with.offset(-kScaleNum(9/2));
    }];
    
    UIImageView *passwordImage = [[UIImageView alloc]init];
    passwordImage.image = [UIImage imageNamed:@"password"];
    [self.view addSubview:passwordImage];
    [passwordImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView2.mas_bottom).with.offset(kScaleNum(20));
        make.size.mas_equalTo(CGSizeMake(kScaleNum(12), kScaleNum(16)));
        make.left.equalTo(self.view).with.offset(kScaleNum(35));
    }];
    
    UITextField *passwordTF = [[UITextField alloc]init];
    [passwordTF addTarget:self action:@selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
    passwordTF.placeholder = @"请输入密码";
    passwordTF.tag = 402;
    passwordTF.textColor = [UIColor colorWithHexString:@"333333"];
    passwordTF.secureTextEntry = YES;
    passwordTF.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:passwordTF];
    [passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(passwordImage.mas_right).with.offset(kScaleNum(15));
        make.height.equalTo(passwordImage);
        make.right.equalTo(self.view).with.offset(-kScaleNum(35));
        make.bottom.equalTo(passwordImage.mas_bottom).with.offset(1);
    }];
    _passwordTF = passwordTF;
    
    UIView *lineView3 = [[UIView alloc]init];
    lineView3.backgroundColor = [UIColor colorWithHexString:@"e5e5e5"];
    [self.view addSubview:lineView3];
    [lineView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(passwordImage.mas_left);
        make.right.equalTo(passwordTF.mas_right);
        make.height.equalTo(@1);
        make.top.equalTo(passwordImage.mas_bottom).with.offset(kScaleNum(5));
    }];
    
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    submitBtn.layer.cornerRadius = kScaleNum(30)/2;
    submitBtn.clipsToBounds = YES;
    [submitBtn addTarget:self action:@selector(submitClick) forControlEvents:UIControlEventTouchUpInside];
    [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    submitBtn.layer.borderWidth = 1;
    submitBtn.layer.borderColor = [UIColor colorWithHexString:@"cccccc"].CGColor;

    submitBtn.backgroundColor = kWhiteColor;
    [submitBtn setTitleColor:[UIColor colorWithHexString:@"cccccc"] forState:UIControlStateNormal];
    submitBtn.layer.borderColor = [UIColor colorWithHexString:@"cccccc"].CGColor;
    submitBtn.userInteractionEnabled = NO;
    
    submitBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:submitBtn];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.height.equalTo(@(kScaleNum(30)));
        make.width.equalTo(@(kScaleNum(200)));
        make.top.equalTo(lineView3.mas_bottom).with.offset(kScaleNum(21));
    }];
    _submitBtn = submitBtn;
   
}
-(void)getCode:(UIButton *)button
{
    if (![self.phoneTF.text checkTel]) {
        showAlert(nil, @"手机号格式不正确");
        return;
    }
    button.userInteractionEnabled = NO;
    __block int time = 10;
    NSTimeInterval period = 1.0;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            time--;
            [button setTitle:[NSString stringWithFormat:@"%ds",time] forState:UIControlStateNormal];
            
            if (time == -1) {
                if(_timer){
                    dispatch_source_cancel(_timer);
                    _timer = nil;
                }
                button.userInteractionEnabled = YES;
                [button setTitle:[NSString stringWithFormat:@"重发"] forState:UIControlStateNormal];
                time = 10;
            }
        });
        
    });
    dispatch_resume(_timer);

    [AllProjectRequest codeWithUserPhone:self.phoneTF.text success:^(id responseObject) {
        
    } failure:^(NSString *error) {
        
    }];
}
-(void)textFieldDidChange
{
    UITextField *phoneTF = [self.view viewWithTag:400];
    UITextField *codeTF = [self.view viewWithTag:401];
    UITextField *passwordTF = [self.view viewWithTag:402];
    if ([phoneTF.text checkTel] && codeTF.text.length == 6 && [passwordTF.text checkPassword]) {
        self.submitBtn.backgroundColor = [UIColor colorWithHexString:@"ff9a02"];
        [self.submitBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
        self.submitBtn.layer.borderColor = kClearColor.CGColor;
        self.submitBtn.userInteractionEnabled = YES;
    }
    else
    {
        self.submitBtn.backgroundColor = kWhiteColor;
        [self.submitBtn setTitleColor:[UIColor colorWithHexString:@"cccccc"] forState:UIControlStateNormal];
        self.submitBtn.layer.borderColor = [UIColor colorWithHexString:@"cccccc"].CGColor;
        self.submitBtn.userInteractionEnabled = NO;
    }
}
-(void)submitClick
{

    [AllProjectRequest forgetPasswordWithUserPhone:self.phoneTF.text userpwd:self.passwordTF.text mobileCode:self.codeTF.text success:^(id responseObject) {
        /*
            成功修改密码之后进入登录页面
         */

        [self.navigationController popViewControllerAnimated:YES];
        
    } failure:^(NSString *error) {
        
    }];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
